﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// Role 的摘要说明。
    /// </summary>
    public class Role
    {
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="rolename">角色名</param>
        /// <param name="roledescription">角色描述</param>
        /// <returns>角色ID</returns>
        public static int Add(string rolename, string roledescription)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Role_Name", SqlDbType.NVarChar, 300, rolename.Trim()),
                Database.MakeInParam("@Role_Description", SqlDbType.NVarChar, 300, roledescription.Trim())
            };
            return (db.RunProc("dbo.sproc_AddMyRole", parameters));
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <returns>0成功1不成功</returns>
        public static int Delete(int roleid)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@role_id", SqlDbType.Int, 4, roleid)
            };
            return (db.RunProc("dbo.sproc_DeleteRole", parameters));
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <param name="rolename">角色名称</param>
        /// <param name="roledescription">角色描述</param>
        /// <returns>返回值（0成功1不成功）</returns>
        public static int Modify(int roleid, string rolename, string roledescription)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@role_id", SqlDbType.Int, 4, roleid),
                Database.MakeInParam("@role_name", SqlDbType.NVarChar, 300, rolename.Trim()),
                Database.MakeInParam("@role_description", SqlDbType.NVarChar, 300, roledescription.Trim())
            };
            return (db.RunProc("dbo.sproc_UpdateRoleInfo", parameters));
        }

        /// <summary>
        /// 从角色中删除成员
        /// </summary>
        /// <param name="roleid">角色id</param>
        /// <param name="staffid">成员id</param>
        /// <returns>返回值</returns>
        public static int DelStaffFromRole(int roleid, string staffid)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Role_ID", SqlDbType.Int, 4, roleid),
                Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 3000, staffid)
            };
            return (db.RunProc("dbo.sproc_DeleteStaffFromRole", parameters));
        }

        /// <summary>
        /// 从角色中添加成员
        /// </summary>
        /// <param name="roleid">角色id</param>
        /// <param name="staffid">非成员id</param>
        /// <returns>返回值</returns>
        public static int AddStaffFromRole(int roleid, string staffid)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@Role_ID", SqlDbType.Int, 4, roleid),
                Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 3000, staffid)
            };
            return (db.RunProc("dbo.sproc_AddStaffToRole", parameters));
        }

        /// <summary>
        /// 根据rolename得到roleid
        /// </summary>
        /// <param name="rolename">角色名称</param>
        /// <returns>角色id</returns>
        public static int GetRoleIDByName(string rolename)
        {
            var db = new Database();
            int roleid = 0;
            var parameters = new[]
            {
                Database.MakeInParam("@rolename", SqlDbType.NVarChar, 100, rolename),
            };
            using (var dr = db.RunDataReader("dbo.sproc_GetRoleIDFromName", parameters))
            {
                while (dr.Read())
                {
                    roleid = Int32.Parse(dr["role_id"].ToString());
                }
            }

            return roleid;
        }

        public SqlDataReader GetRoleInfo(long RoleID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@RoleID", SqlDbType.Int, 4, RoleID),
            };
            return db.RunDataReader("dbo.sproc_GetRoleInfo", parameters);
        }

        public string GetRoleInfo(long RoleID, string FiledName)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@RoleID", SqlDbType.Int, 4, RoleID),
            };
            using (var dr = db.RunDataReader("dbo.sproc_GetRoleInfo", parameters))
            {
                if (dr.Read())
                {
                    try
                    {
                        return dr[FiledName].ToString();
                    }
                    catch
                    {
                        return "";
                    }
                }
                return "";
            }
        }
    }
}